package com.fly.control;

import java.sql.ResultSet;

import com.fly.conn.DBConnect;
import com.fly.model.Classify;

public class ClassifyControl {
	public static boolean insertClassify(Classify c){
		DBConnect dbc = new DBConnect();
		String sql = "insert into paramclassify(name) values (?)";
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, c.getName());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("insertClassify ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean deleteClassify(int id){
		String sql = "delete * from paramclassify where id =" + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("deleteClassify ERROR");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateClassify(int id, Classify c){
		String sql = "update paramclassify set name = ? where id = " + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, c.getName());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("updateClassify ERROR");
		} finally{
			dbc.close();
		} 
		return false;
	}
	public static Classify getClassify(int id){
		String sql = "select * from paramclassify where id =" + id;
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		Classify c = new Classify();
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				c.setName(rs.getString("name"));
			}
			return c;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("getClassify ERROR");
		} finally{
			dbc.close();
		}
		return null;
	}
}
